﻿using System.Collections.Generic;
using System.Linq;
using S7PlcDataCollector.Database.SQLite;

namespace S7PlcDataCollector.Service.SQLiteProxy.Dal
{
    internal class S7PlcVariableEntity
    {
        private static readonly string TableName = SqliteHelper.TableS7PlcVariableEntity;

        /// <summary>
        /// 添加PLC变量实体
        /// </summary>
        /// <param name="model">PLC变量实体</param>
        /// <returns>Rid</returns>
        public int Insert(Models.S7PlcVariableEntity model)
        {
            if (model == null)
            {
                return 0;
            }

            var sql = $"insert into {TableName}(S7PlcEntityRid, Priority, Variable, VariableType, Remark) " +
                      "values(@S7PlcEntityRid, @Priority, @Variable, @VariableType, @Remark);";
            return SqliteDbHelper.Insert(sql, model);
        }

        /// <summary>
        /// 批量添加PLC变量实体
        /// </summary>
        /// <param name="models">PLC变量实体集合</param>
        /// <returns>true成功，false失败</returns>
        public bool Insert(List<Models.S7PlcVariableEntity> models)
        {
            if (models == null || models.Count == 0)
            {
                return false;
            }

            var sql = $"insert into {TableName}(S7PlcEntityRid, Priority, Variable, VariableType, Remark) " +
                      "values(@S7PlcEntityRid, @Priority, @Variable, @VariableType, @Remark);";
            return SqliteDbHelper.Insert(sql, models.ToList<object>());
        }

        /// <summary>
        /// 更新PLC变量实体(不修改PLC变量实体Id)
        /// </summary>
        /// <param name="model">PLC变量实体</param>
        /// <returns>true成功，false失败</returns>
        public bool Update(Models.S7PlcVariableEntity model)
        {
            if (model == null)
            {
                return false;
            }

            var sql = $"update {TableName} " +
                      "set S7PlcEntityRid = @S7PlcEntityRid, Priority = @Priority, " +
                      "    Variable = @Variable, VariableType = @VariableType, Remark = @Remark " +
                      "where Rid = @Rid;";
            return SqliteDbHelper.Update(sql, model);
        }

        /// <summary>
        /// 删除PLC变量实体
        /// </summary>
        /// <param name="model">PLC变量实体</param>
        /// <returns>true成功，false失败</returns>
        public bool Delete(Models.S7PlcVariableEntity model)
        {
            if (model == null)
            {
                return false;
            }

            //级联删除
            var sql = "PRAGMA foreign_keys=ON;" +
                      $"delete from {TableName} where Rid = @Rid;";
            return SqliteDbHelper.Delete(sql, model);
        }

        /// <summary>
        /// 查询PLC变量实体
        /// </summary>
        /// <param name="rid">PLC变量实体Id</param>
        /// <returns>PLC变量实体</returns>
        public Models.S7PlcVariableEntity GetModel(int rid)
        {
            var sql = $"select * from {TableName} where Rid = @Rid";
            var model = new Models.S7PlcVariableEntity() {Rid = rid};
            return SqliteDbHelper.GetDataList<Models.S7PlcVariableEntity>(sql, model).FirstOrDefault();
        }

        /// <summary>
        /// 查询PLC变量实体集合
        /// </summary>
        /// <param name="s7PlcEntityRid">PLC实体Id</param>
        /// <returns>PLC变量实体集合</returns>
        public List<Models.S7PlcVariableEntity> GetModels(int s7PlcEntityRid)
        {
            var sql = $"select * from {TableName} where S7PlcEntityRid = @S7PlcEntityRid";
            var model = new Models.S7PlcVariableEntity() { S7PlcEntityRid = s7PlcEntityRid };
            return SqliteDbHelper.GetDataList<Models.S7PlcVariableEntity>(sql, model);
        }

        /// <summary>
        /// 查询所有PLC变量实体
        /// </summary>
        /// <returns>PLC变量实体集合</returns>
        public List<Models.S7PlcVariableEntity> GetAllModels()
        {
            var sql = $"select * from {TableName}";
            return SqliteDbHelper.GetDataList<Models.S7PlcVariableEntity>(sql);
        }
    }
}
